package com.mocai.merchant.service;

import com.mocai.merchant.core.service.BaseService;
import com.mocai.merchant.mapper.UserMapper;
import com.mocai.merchant.model.User;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/1/7.
 */
@Service
public class UserService extends BaseService<UserMapper, User, Integer> {
    @Autowired
    UserMapper userMapper;

    public User findByUsernameAndPassword(String username, String password) {
        return userMapper.selectByUsernameAndPassword(username, password);
    }

    public User findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    public void txTest2(){
        User user = userMapper.selectByPrimaryKey(50);
        user.setUsername("111");
        userMapper.updateById(user);
//        userMapper.deleteById(51);
//        userMapper.findById(51);
    }

    public void txTest() {
        method1();
        method2();
//        int i = 1/0;
    }

    public void method1() {
        User user = userMapper.selectByPrimaryKey(50);
        user.setUsername(user.getUsername() + "-updated");
        userMapper.updateByPrimaryKey(user);
    }

    public void method2() {
        User user = userMapper.selectByPrimaryKey(51);
        user.setUsername(user.getUsername() + "-updated");
        userMapper.updateByPrimaryKey(user);
        throw new RuntimeException("method2异常==>导致method1事务回滚");
    }

//    public int save(List<User> users) {
//        template.getSqlSessionFactory().openSession();
//        SqlSession sqlSession = template.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
//        Connection connection = sqlSession.getConnection();
//        String sql = "insert into t_user (username, password) values(?, ?)";
//        try {
//            connection.setAutoCommit(false);
//            PreparedStatement ps = connection.prepareStatement(sql);
//            int counter = 1;
//            for (User user : users) {
//                ps.setObject(1, user.getUsername());
//                ps.setObject(2, user.getPassword());
//                if (counter % 50 == 0) {
//                    ps.addBatch();
//                    ps.executeBatch();
//                }
//                counter++;
//            }
//            ps.executeBatch();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                connection.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//        return 1;
//    }

//    public int save(List<User> users) {
//        SqlSession sqlSession = template.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
//        try {
//            for (int i = 0; i < users.size(); i++) {
//                sqlSession.insert(UserMapper.class.getName() + ".insert", users.get(i));
//                if (i % 50 == 0 && i > 0) {
//                    sqlSession.flushStatements();
//                    // 提交事物
//                    sqlSession.commit();
//                    // 清空缓存，防止溢出
//                    sqlSession.clearCache();
//                }
//            }
//            sqlSession.commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//            sqlSession.rollback();
//        } finally {
//            sqlSession.close();
//        }
//
//        return 1;
//    }
}
